package com.nuanshui.heatedloan.controller.display.credit;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.framework.security.DESC;
import com.nuanshui.heatedloan.constant.DubboCacheRegion;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.controller.BaseController;
import com.nuanshui.heatedloan.credit.constant.AuditResultType;
import com.nuanshui.heatedloan.credit.facade.AllinPayTransService;
import com.nuanshui.heatedloan.credit.facade.CustomerBankCardInfoService;
import com.nuanshui.heatedloan.credit.facade.CustomerCreditResultService;
import com.nuanshui.heatedloan.credit.form.CustomerBankCardInfoForm;
import com.nuanshui.heatedloan.credit.response.RiskResponse;
import com.nuanshui.heatedloan.credit.vo.CustomerBankCardInfoVO;
import com.nuanshui.heatedloan.credit.vo.CustomerCreditResultVO;
import com.nuanshui.heatedloan.dubbo.DubboResponse;
import com.nuanshui.heatedloan.dubbo.DubboResponseCode;
import com.nuanshui.heatedloan.dubbo.extension.cache.manager.DubboCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangxw on 2017/6/27.
 */

@RequestMapping("credit/bankCard")
@RestController
public class CustomerBankCardInfoController extends BaseController {

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private CustomerBankCardInfoService customerBankCardInfoService;

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private CustomerCreditResultService customerCreditResultService;

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private AllinPayTransService allinPayTransService;

    @Autowired
    private DubboCacheManager dubboCacheManager;

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public String addCustomerBankCardInfo(String customerBankCardInfoMsg){
        RiskResponse riskResponse=new RiskResponse();
        if(SysStringUtils.isEmpty(customerBankCardInfoMsg)){
            return error("请传入参数");
        }
        String userId= getCurrentUser().getId();
        CustomerBankCardInfoForm customerBankCardForm= JSON.parseObject(customerBankCardInfoMsg,CustomerBankCardInfoForm.class);
        CustomerBankCardInfoVO vo = customerBankCardInfoService.loadVOByMixCardNo(customerBankCardForm.getMixCardNo());
        if(vo != null ){
            return error("该银行卡已存在，不可重复添加");
        }else{
            int count= customerBankCardInfoService.loadCountByUserId(userId);
            if (count>=20){
                return error("用户最多只能绑定20张银行卡");
            }
            customerBankCardForm.setUserId(userId);
            customerBankCardForm.setStatus("1");
            riskResponse=customerBankCardInfoService.saveCustomerBankCardInfo(customerBankCardForm);
        }
        return success(riskResponse);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public String deleteCustomerBankCardInfo(String id){
        String userId= getCurrentUser().getId();
        int count= customerBankCardInfoService.loadCountByUserId(userId);
        if(count ==1){
            return error("用户至少要绑定一张银行卡");
        }
        customerBankCardInfoService.deleteCustomerBankCardInfo(id);
        dubboCacheManager.evictCache(DubboCacheRegion.CACHE_CONSTANT_BANKINFO_LIST,userId);
        return success();
    }

    @RequestMapping(value = "/load",method = RequestMethod.POST)
    public String loadCustomerBankCardInfoVO(String id){
        CustomerBankCardInfoVO vo = customerBankCardInfoService.loadVO(id);
        return success(vo);
    }

    @RequestMapping(value = "/loadByUserId",method = RequestMethod.POST)
    public String loadCustomerBankCardInfoVOByUserId(){
        String userId= getCurrentUser().getId();
        Map<String,Object> resultMap = new HashMap<String,Object>();
        CustomerCreditResultVO vo = customerCreditResultService.loadVOByUserId(userId);
        if(vo == null ){
            resultMap.put("auditStatus",AuditResultType.BEFORE_AUDIT);
        }else if(vo.getStatus() == AuditResultType.AUDITING){
            resultMap.put("auditStatus",AuditResultType.AUDITING);
        }else {
            resultMap.put("auditStatus",AuditResultType.AFTER_AUDITED);
        }
        List<CustomerBankCardInfoVO> list = (List<CustomerBankCardInfoVO>)dubboCacheManager.get(DubboCacheRegion.CACHE_CONSTANT_BANKINFO_LIST, userId);
        if (list == null){
            list =customerBankCardInfoService.loadCustomerBankCardInfoVOByUserId(userId);
            dubboCacheManager.put(DubboCacheRegion.CACHE_CONSTANT_BANKINFO_LIST,list,userId);
        }
        resultMap.put("list",list);
        return success(resultMap);
    }

    @RequestMapping(value = "/loadLatestByUserId",method = RequestMethod.POST)
    public String loadLatestVOByUserId(){
        CustomerBankCardInfoVO customerBaseInfoVO =customerBankCardInfoService.loadLatestVOByUserId(getCurrentUser().getId());
        return success(customerBaseInfoVO);
    }

    @RequestMapping(value = "/queryByAllinPay",method = RequestMethod.POST)
    public String queryByAllinPay(String mixBankNo) {

        if (SysStringUtils.isNotEmpty(mixBankNo)) {
            try {
                DubboResponse response = allinPayTransService.queryBankCard(DESC.decrypt3DES(mixBankNo));
                return success(response);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return success();
    }
}
